<?php
declare(strict_types=1);
namespace App\Service;

use App\Amqp\Producer\MailProducer;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\User;
use Hyperf\Amqp\Producer;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\SimpleCache\CacheInterface;

class IndexService
{


    /**
     * 获取用户详情
     * @return array|mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function info3()
    {
        $uid = 2;

        $container = ApplicationContext::getContainer();
        $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
        $userKey = sprintf("userInfo:%d", $uid);
        $userInfo = $cache->get($userKey);
        if (is_null($userInfo)) {
            $userModel = new User();
            $userInfo = $userModel->getUserInfo($uid);
            $cache->set($userKey, $userInfo, 60);
        }
        return $userInfo;
    }

    /**
     * 获取用户详情
     * @return array|mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function info4()
    {
        $uid = 2;

//        $container = ApplicationContext::getContainer();
//        $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
        $cache = make(CacheInterface::class, ['driver' => 'other']);
        $userKey = sprintf("userInfo:%d", $uid);
        $userInfo = $cache->get($userKey);
        if (is_null($userInfo)) {
            $userModel = new User();
            $userInfo = $userModel->getUserInfo($uid);
            $cache->set($userKey, $userInfo, 60);
        }
        return $userInfo;
    }

    public function info(int $id)
    {
        if ($id <= 0) {
            throw new \Exception("id无效");
        }
        return ['info' => 'data info'];
    }
    public function info2(int $id)
    {

        if ($id <= 0) {
//            $translator = ApplicationContext::getContainer()->get(TranslatorInterface::class);
//            $translator->setLocale('ja');
//            throw new BusinessException(trans('params.id_invalid'));
            throw new BusinessException(ErrorCode::PARAMS_ID_INVALID);
//            throw new BusinessException(ErrorCode::getMessage(ErrorCode::PARAMS_ID_INVALID));
//            throw new BusinessException("id无效");
        }
        return ['info' => 'data info'];
    }

    public function mail()
    {
        $startTime = microtime(true);

        $mail = ApplicationContext::getContainer()->get(\App\Components\Mail::class);
        // ->to 这里填写目标邮箱，大家自己更改，不要都写我的了
        $mail->to('hang7872@163.com')->send('邮件测试标题', '<b style="color: #f00;">邮件测试内容</b>');

        $runTime = '耗时: ' . (microtime(true) - $startTime) . ' s';

        return ['runtime' => $runTime];
    }

    /**
     * 使用协程 记录时间
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function mail2()
    {
        $startTime = microtime(true);
        co(function () {
            $mail = ApplicationContext::getContainer()->get(\App\Components\Mail::class);
            $mail->to('hang7872@163.com')->send('邮件测试标题', '<b style="color: #f00;">邮件测试内容</b>');
            $log = ApplicationContext::getContainer()->get(LoggerFactory::class)->get('co');
            $log->info("邮件已发送.");
        });
        $runTime = '耗时: ' . (microtime(true) - $startTime) . ' s';
        return ['time' => date('Y-m-d H:i:s'), 'runtime' => $runTime];
    }

    public function mail3(){
        $startTime = microtime(true);

        $mailInfo = [
            'to' => 'hang7872@163.com',
            'subject' => '邮件测试标题111',
            'body' => '<b style="color: #f00;">mq测试</b>',
        ];
        $message = new MailProducer($mailInfo);
        $producer = ApplicationContext::getContainer()->get(Producer::class);
        $producer->produce($message);

        $runTime = '耗时: ' . (microtime(true) - $startTime) . ' s';

        return ['time' => date('Y-m-d H:i:s'), 'runtime' => $runTime];
    }
}